package cn.monkey.family.data.shop.order;

import cn.monkey.family.data.EntityMapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.ReportingPolicy;
import org.springframework.util.CollectionUtils;

import javax.inject.Inject;
import java.util.Collection;
import java.util.Collections;

@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public abstract class OrderMapper extends EntityMapper<OrderDto, Order, OrderVo> {

    @Inject
    OrderDetailSelector orderDetailSelector;

    @Named("getDetails")
    public Collection<OrderDetail> getDetails(Collection<String> details) {
        if (CollectionUtils.isEmpty(details)) {
            return Collections.emptyList();
        }
        return this.orderDetailSelector.selectByIds(details);
    }


    @Override
    @Mapping(target = "details", source = "details", qualifiedByName = "getDetails")
    @Mapping(target = "creator", source = "creatorId", qualifiedByName = "getUserIdAndNamePairFromId")
    @Mapping(target = "updater", source = "updaterId", qualifiedByName = "getUserIdAndNamePairFromId")
    public abstract OrderVo toVo(Order order);


    @Override
    @Mapping(target = "details", expression = "java(orderDto.getDetails() == null? null: orderDto.getDetails().stream().map(cn.monkey.family.data.shop.order.OrderDetailDto::getSkuId).toList())")
    public abstract Order toEntity(OrderDto orderDto);
}
